package com.jiankangjin.alipaychannel.services.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.jiankangjin.alipaychannel.alipay.config.AlipayConfig;
import com.jiankangjin.alipaychannel.alipay.util.AlipayCore;
import com.jiankangjin.alipaychannel.services.QueryService;
import com.jiankangjin.alipaychannel.util.XmlUtil;
import com.jiankangjin.alipaychannel.vo.QueryVo;
import com.jiankangjin.alipaychannel.vo.QueryXmlBean;
import com.jiankangjin.httpclient.HttpClient;


@Service
public class QueryServiceImpl implements QueryService {
	private static final transient Log logger = LogFactory.getLog(QueryServiceImpl.class);
	
	public static void main(String[] args) {
		new QueryServiceImpl().barcode_V0_0_1(
				"20140901155918713-000024eE9jbDFr", "", 
				"2014090121001004760020628651", "");
	}
	
	/**
	 * 支付宝条码付订单查询
	 */
	@Override
	public QueryVo barcode_V0_0_1(
			String pay_ssn_id,
			String order_id, 
			String pay_chnl_seq, 
			String expand_json) {
		//整理参数
		Map<String, String> sParaTemp = new HashMap<String, String>();
		sParaTemp.put("service", "single_trade_query");
        sParaTemp.put("partner", AlipayConfig.PARTNER);
        sParaTemp.put("_input_charset", "utf-8");
		sParaTemp.put("trade_no", pay_chnl_seq);
		sParaTemp.put("out_trade_no", pay_ssn_id);
		
		//用支付宝的类和SDK进行RSA签名
		String privateKey = AlipayConfig.PRIVATE_KEY;
        Map<String, String> sPara = AlipayCore.buildSignPara(sParaTemp, privateKey);
        Map<String, Object> ssPara = new HashMap<String, Object>();
        for (String key : sPara.keySet()) {
			ssPara.put(key, sPara.get(key));
		}
        logger.info(ssPara);
        HttpClient httpClient = HttpClient.getInstance();
    	String service = "https://mapi.alipay.com/gateway.do?";
    	//提交到支付宝得到XML返回数据
    	String XMLstr = httpClient.doPOSTgetStr(service, ssPara);
    	//用dom4j的方式 把XML转为对象
    	QueryXmlBean queryXmlBean = XmlUtil.parseQueryVo(XMLstr);
    	
    	QueryVo queryVo = new QueryVo();
    	String tradeStat = queryXmlBean.getTradeStatus();
    	System.out.println(tradeStat+" 1111111111-------------------->>>");
    	if ("TRADE_SUCCESS".equals(tradeStat) || "TRADE_FINISHED".equals(tradeStat)) {
    		queryVo.setPay_fin_time(this.convertTime(queryXmlBean.getGmtPayment()));
    		queryVo.setPay_chnl_mer(queryXmlBean.getSellerEmail());
    		queryVo.setPay_user(queryXmlBean.getBuyerEmail());
    		queryVo.setSub_deal_status("1001");
    		queryVo.setSub_deal_msg("交易成功");
		} else if ("TRADE_CLOSED".equals(tradeStat)) {
    		queryVo.setPay_fin_time(this.convertTime(queryXmlBean.getGmtPayment()));
    		queryVo.setPay_chnl_mer(queryXmlBean.getSellerEmail());
    		queryVo.setPay_user(queryXmlBean.getBuyerEmail());
    		queryVo.setSub_deal_status("0071");
    		queryVo.setSub_deal_msg("交易关闭");
		} else if("WAIT_BUYER_PAY".equals(tradeStat) || "TRADE_PENDING".equals(tradeStat)) {
    		queryVo.setPay_chnl_mer(queryXmlBean.getSellerEmail());
    		queryVo.setPay_user(queryXmlBean.getBuyerEmail());
    		queryVo.setSub_deal_status("0021");
    		queryVo.setSub_deal_msg("用户正在支付");
		}
    	queryVo.setPay_ssn_id(pay_ssn_id);
    	queryVo.setPay_chnl_seq(pay_chnl_seq);
    	queryVo.setPay_chnl_pos("");
    	queryVo.setPay_chnl_fee("0");
    	queryVo.setResp_status("0000");
    	queryVo.setResp_msg("成功");
    	queryVo.setSign("AlipayChannel_Query_sign");
		return queryVo;
	}

	/**
	 * 把支付宝时间转为14位
	 * 2014-12-11 10:32:57  ->  20141211103257
	 */
	private String convertTime(String gmtPayment) {
		String s = gmtPayment.replace("-", "");
		s = s.replace(" ", "");
		s = s.replace(":", "");
		return s;
	}

	/**
	 * 支付宝公众号支付订单查询
	 */
	@Override
	public QueryVo public_V0_0_1(String pay_ssn_id, String order_id,
			String pay_chnl_seq, String expand_json) {
		return null;
	}

}
